import {FormControl, NG_VALIDATORS} from '@angular/forms';
import {Directive} from '@angular/core';

export function validateEmail(c: FormControl) {
  let EMAIL_REGEXP = /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;

  return EMAIL_REGEXP.test(c.value) ? null : {
      validateEmail: {valid: false}
    }
}

@Directive({
  selector: '[validateEmail][ngModel]',
  providers: [
    {provide: NG_VALIDATORS, useValue: validateEmail, multi: true}
  ]
})
export class EmailValidator {}
